//
//  MeTopTableViewCell.m
//  Cat
//
//  Created by lushitong on 2018/11/29.
//  Copyright © 2018年 lushitong. All rights reserved.
//

#import "MeTopTableViewCell.h"
#import "FLHeadView.h"
#import "FMDBUser.h"
#import "IncomeViewController.h"
#import "FansViewController.h"
#import "LoginViewController.h"

@interface MeTopTableViewCell()

@property (nonatomic, strong) UIImageView *backImagView;
@property (nonatomic, strong) FLHeadView *headView;
@property (nonatomic, strong) UILabel *textIDLabel;
@property (nonatomic, strong) UIImageView *idinifImageView;
@property (nonatomic, strong) UILabel *inviteNumberLabel;
@property (nonatomic, strong) UIButton *checkButton;

// 设置下面的View
@property (nonatomic, strong) UIImageView *operView;
@property (nonatomic, strong) UILabel *moneyLabel;
@property (nonatomic, strong) UIImageView *leftArrowImageView;
@property (nonatomic, strong) UILabel *moneyInfoLabel;
@property (nonatomic, strong) UILabel *thisLabel;
@property (nonatomic, strong) UILabel *lastLabel;
@property (nonatomic, strong) UIView *shuView;
@property (nonatomic, strong) UILabel *fanLabel;
@property (nonatomic, strong) UIImageView *rightArrowImageView;
@property (nonatomic, strong) UILabel *fanNumebrLabel;

@property (nonatomic, strong) UIButton *leftButton;
@property (nonatomic, strong) UIButton *rightButton;

@property (nonatomic, strong) UIButton *copyButton;
@property (nonatomic, strong) FMDBUser *user;

@end

@implementation MeTopTableViewCell

- (void)setCellModelData:(id)cellModelData {
	if ([cellModelData isKindOfClass:[FMDBUser class]]) {
		FMDBUser *user = (FMDBUser *)cellModelData;
		_user = user;
		[self.headView setImageUrl:[NSURL URLWithString:user.avator] size:CGSizeMake(kQKFitModuleDecimal(40), kQKFitModuleDecimal(40)) cornerRadius:kQKFitModuleDecimal(20)];
		if (UDToken) {
			self.textIDLabel.text = user.nickName.length == 0 ? UDPhone : user.nickName;
			[self.textIDLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
				make.left.equalTo(self.headView.mas_right).offset(kQKFitModuleDecimal(11));
				make.top.equalTo(self.headView.mas_top);
			}];
		} else {
			self.textIDLabel.text = @"欢迎您，请登录";
			[self.textIDLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
				make.left.equalTo(self.headView.mas_right).offset(kQKFitModuleDecimal(11));
				make.centerY.equalTo(self.headView.mas_centerY);
			}];
		}
		self.inviteNumberLabel.text = [NSString stringWithFormat:@"邀请码 %@",[user.inviteCode validateString] ? user.inviteCode : @"0"];
		self.moneyInfoLabel.text = [NSString stringWithFormat:@"￥%@",[user.balance validateString] ? user.balance :@"0.0"];
		self.thisLabel.text = [NSString stringWithFormat:@"本月预估 ￥%@",[user.expectAmount validateString] ? user.expectAmount :@"0.0"];
		self.lastLabel.text = [NSString stringWithFormat:@"上月结算 ￥%@",[user.lastAmount validateString] ? user.lastAmount :@"0.0"];
		self.fanNumebrLabel.text = [NSString stringWithFormat:@"%.lf",user.fansNum.doubleValue];
		if (!UDToken) {
			self.idinifImageView.hidden = YES;
			self.inviteNumberLabel.hidden = YES;
			self.checkButton.hidden = YES;
		} else {
			self.idinifImageView.hidden =
			self.inviteNumberLabel.hidden =
			self.checkButton.hidden = NO;
		}
	}
}

- (void)updateCoverWithOffset:(CGFloat)offset {
	self.backImagView.y = offset;
	self.backImagView.height = kQKFitModule(195) + fabs(offset) + StatusBarSafeInsetHeight;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
	self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
	if (self) {
		self.cellSeparatorStyle = QKUITableViewCellSeparatorStyleNone;
		self.selectionStyle = UITableViewCellSelectionStyleNone;
		self.userInteractionEnabled = YES;
		[self loadSubviews];
	}
	return self;
}

- (void)loadSubviews {
	
	[self.contentView addSubview:self.backImagView];
	[self.contentView addSubview:self.headView];
	[self.contentView addSubview:self.textIDLabel];
	[self.contentView addSubview:self.idinifImageView];
	[self.contentView addSubview:self.inviteNumberLabel];
	[self.contentView addSubview:self.checkButton];
	[self.contentView addSubview:self.operView];
	[self.contentView addSubview:self.copyButton];
	
	[self.operView addSubviews:@[self.moneyLabel,
								 self.leftArrowImageView,
								 self.moneyInfoLabel,
								 self.thisLabel,
								 self.lastLabel,
								 self.shuView,
								 self.fanLabel,
								 self.rightArrowImageView,
								 self.fanNumebrLabel,
								 self.leftButton,
								 self.rightButton]];
	
	[self.backImagView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.top.and.right.equalTo(self);
		make.height.equalTo(@(kQKFitModule(195)));
	}];

	[self.headView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.mas_equalTo(CGSizeMake(kQKFitModuleDecimal(40), kQKFitModuleDecimal(40)));
		make.left.equalTo(self.contentView.mas_left).offset(kQKFitModuleDecimal(18));
		make.top.equalTo(self.contentView.mas_top).offset(kQKFitModuleDecimal(80));
	}];

	[self.textIDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.headView.mas_right).offset(kQKFitModuleDecimal(11));
		make.top.equalTo(self.headView.mas_top);
	}];

	[self.idinifImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(60), kQKFitModuleDecimal(16)));
		make.left.equalTo(self.textIDLabel.mas_right).offset(kQKFitModuleDecimal(4));
		make.centerY.equalTo(self.textIDLabel.mas_centerY);
	}];

	[self.inviteNumberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.textIDLabel.mas_left);
		make.bottom.equalTo(self.headView.mas_bottom);
	}];

	[self.checkButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(20), kQKFitModuleDecimal(11)));
		make.centerY.equalTo(self.inviteNumberLabel.mas_centerY);
		make.left.equalTo(self.inviteNumberLabel.mas_right).offset(kQKFitModuleDecimal(10));
	}];

	[self.operView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.contentView.mas_left).offset(kQKFitModuleDecimal(16));
		make.top.equalTo(self.headView.mas_bottom).offset(kQKFitModuleDecimal(17));
	}];
	
	
	[self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.operView.mas_top).offset(kQKFitModuleDecimal(18));
		make.left.equalTo(self.operView.mas_left).offset(kQKFitModuleDecimal(66));
	}];
	
	[self.leftArrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(5), kQKFitModuleDecimal(8)));
		make.centerY.equalTo(self.moneyLabel.mas_centerY);
		make.left.equalTo(self.moneyLabel.mas_right).offset(kQKFitModuleDecimal(4));
	}];
	
	[self.moneyInfoLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.top.equalTo(self.moneyLabel.mas_bottom).offset(kQKFitModuleDecimal(12));
		make.centerX.equalTo(self.moneyLabel.mas_centerX);
	}];
	
	[self.thisLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerX.equalTo(self.moneyInfoLabel.mas_centerX);
		make.top.equalTo(self.moneyInfoLabel.mas_bottom).offset(kQKFitModuleDecimal(9));
	}];
	
	[self.lastLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerX.equalTo(self.moneyInfoLabel.mas_centerX);
		make.top.equalTo(self.thisLabel.mas_bottom).offset(kQKFitModuleDecimal(2));
	}];
	
	[self.shuView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerX.equalTo(self.operView.mas_centerX);
		make.width.equalTo(@(kQKFitModuleDecimal(1)));
		make.top.equalTo(self.operView.mas_top).offset(kQKFitModuleDecimal(18));
		make.bottom.equalTo(self.operView.mas_bottom).offset(-kQKFitModuleDecimal(18));
	}];

	[self.fanLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.right.equalTo(self.operView.mas_right).offset(-kQKFitModuleDecimal(77));
		make.centerY.equalTo(self.moneyLabel.mas_centerY);
	}];

	[self.rightArrowImageView mas_makeConstraints:^(MASConstraintMaker *make) {
		make.size.equalTo(CGSizeMake(kQKFitModuleDecimal(5), kQKFitModuleDecimal(8)));
		make.centerY.equalTo(self.fanLabel.mas_centerY);
		make.left.equalTo(self.fanLabel.mas_right).offset(kQKFitModuleDecimal(4));
	}];

	[self.fanNumebrLabel mas_makeConstraints:^(MASConstraintMaker *make) {
		make.centerX.equalTo(self.fanLabel.mas_centerX);
		make.top.equalTo(self.fanLabel.mas_bottom).offset(kQKFitModuleDecimal(12));
	}];
	
	[self.leftButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.and.top.and.bottom.equalTo(self.operView);
		make.right.equalTo(self.shuView.mas_left);
	}];
	
	[self.rightButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.right.and.top.and.bottom.equalTo(self.operView);
		make.left.equalTo(self.shuView.mas_right);
	}];
	
	[self.copyButton mas_makeConstraints:^(MASConstraintMaker *make) {
		make.left.equalTo(self.operView.mas_left);
		make.top.equalTo(self.headView.mas_top);
		make.bottom.equalTo(self.operView.mas_top);
		make.width.equalTo(@(ScreenWidth / 2));
	}];
}

#pragma mark----- 私有方法 -----

- (void)pushImcome {
	if (UDToken.length == 0) {
		LoginViewController *login = [[LoginViewController alloc] init];
		[self.currentNavigationController pushViewController:login animated:YES];
		return;
	}
	IncomeViewController *income = [[IncomeViewController alloc] init];
	[self.currentNavigationController pushViewController:income animated:YES];
}

- (void)pushFansVC {
	if (UDToken.length == 0) {
		LoginViewController *login = [[LoginViewController alloc] init];
		[self.currentNavigationController pushViewController:login animated:YES];
		return;
	}
	FansViewController *fans = [[FansViewController alloc] init];
	[self.currentNavigationController pushViewController:fans animated:YES];
}

- (void)pastBordAction {
	UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];
	if (UDToken) {
		pasteboard.string = self.user.inviteCode;
		[FLProgressHUD showSuccess:@"复制成功"];
	} else {
		LoginViewController *login = [[LoginViewController alloc] init];
		[self.currentNavigationController pushViewController:login animated:YES];
	}
}
#pragma mark----- lazy init -----

- (UIImageView *)backImagView {
	if (!_backImagView) {
		_backImagView = [[UIImageView alloc] init];
		_backImagView.backgroundColor = COLOR_WITH_HEX(0xFFC236);
	}
	return _backImagView;
}

- (FLHeadView *)headView {
	if (!_headView) {
		_headView = [[FLHeadView alloc] init];
	}
	return _headView;
}

- (UILabel *)textIDLabel {
	if (!_textIDLabel) {
		_textIDLabel = [UILabel labelWithFont:[UIFont boldSystemFontOfSize:kQKFitFont(21)] textColor:COLOR_WITH_HEX(0x57524F)];
	}
	return _textIDLabel;
}

- (UIImageView *)idinifImageView {
	if (!_idinifImageView) {
		_idinifImageView = [[UIImageView alloc] init];
		_idinifImageView.image = [UIImage imageNamed:@"me_vip_pic"];
	}
	return _idinifImageView;
}

- (UILabel *)inviteNumberLabel {
	if (!_inviteNumberLabel) {
		_inviteNumberLabel = [UILabel labelWithFont:[UIFont boldSystemFontOfSize:kQKFitFont(12)] textColor:COLOR_WITH_HEX(0x57524F)];
		_inviteNumberLabel.text = @"邀请码 1234567";
	}
	return _inviteNumberLabel;
}

- (UIButton *)checkButton {
	if (!_checkButton) {
		_checkButton = [UIButton buttonWithTitle:@"复制" color:COLOR_WITH_HEX(0x57524F) font:[UIFont systemFontOfSize:kQKFitFont(8)]];
		_checkButton.backgroundColor = COLOR_WITH_HEX(0xFFDA84);
	}
	return _checkButton;
}

- (UIButton *)copyButton {
	if (!_copyButton) {
		_copyButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_copyButton addTarget:self action:@selector(pastBordAction) forControlEvents:UIControlEventTouchUpInside];
	}
	return _copyButton;
}

- (UIImageView *)operView {
	if (!_operView) {
		_operView = [[UIImageView alloc] init];
		_operView.image = [UIImage createImageWithSize:CGSizeMake(ScreenWidth - kQKFitModuleDecimal(16) * 2, kQKFitModuleDecimal(118)) gradientColors:@[COLOR_WITH_HEX(0x515359),COLOR_WITH_HEX(0x262628)] percentage:@[@(0),@(1)] gradientType:GradientFromLeftTopToRightBottom];
		_operView.layer.cornerRadius = kQKFitModuleDecimal(6);
		_operView.layer.masksToBounds = YES;
		_operView.userInteractionEnabled = YES;
	}
	return _operView;
}

- (UILabel *)moneyLabel {
	if (!_moneyLabel) {
		_moneyLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0xFFC236)];
		_moneyLabel.userInteractionEnabled = YES;
		UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(pushImcome)];
		[_moneyLabel addGestureRecognizer:tapGes];
		_moneyLabel.text = @"我的余额";
	}
	return _moneyLabel;
}

- (UIImageView *)leftArrowImageView {
	if (!_leftArrowImageView) {
		_leftArrowImageView = [[UIImageView alloc] init];
		_leftArrowImageView.image = [UIImage imageNamed:@"me_arrow"];
	}
	return _leftArrowImageView;
}

- (UILabel *)moneyInfoLabel {
	if (!_moneyInfoLabel) {
		_moneyInfoLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(24)] textColor:COLOR_WITH_HEX(0xFFC236)];
		_moneyInfoLabel.text = @"￥40.00";
	}
	return _moneyInfoLabel;
}

- (UILabel *)thisLabel {
	if (!_thisLabel) {
		_thisLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
		_thisLabel.text = @"本月预估 ￥40.00";
	}
	return _thisLabel;
}

- (UILabel *)lastLabel {
	if (!_lastLabel) {
		_lastLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0xCDC7C4)];
		_lastLabel.text = @"上月结算 ￥40.00";
	}
	return _lastLabel;
}

- (UIView *)shuView {
	if (!_shuView) {
		_shuView = [[UIView alloc] init];
		_shuView.backgroundColor = COLOR_WITH_HEX(0x57524F);
	}
	return _shuView;
}

- (UILabel *)fanLabel {
	if (!_fanLabel) {
		_fanLabel = [UILabel labelWithFont:[UIFont systemFontOfSize:kQKFitFont(10)] textColor:COLOR_WITH_HEX(0xFFC236)];
		_fanLabel.userInteractionEnabled = YES;
		_fanLabel.text = @"我的粉丝";
	}
	return _fanLabel;
}

- (UIImageView *)rightArrowImageView {
	if (!_rightArrowImageView) {
		_rightArrowImageView = [[UIImageView alloc] init];
		_rightArrowImageView.image = [UIImage imageNamed:@"me_arrow"];
	}
	return _rightArrowImageView;
}

- (UILabel *)fanNumebrLabel {
	if (!_fanNumebrLabel) {
		_fanNumebrLabel = [UILabel labelWithFont:[UIFont boldSystemFontOfSize:kQKFitFont(24)] textColor:COLOR_WITH_HEX(0xFFC236)];
		_fanNumebrLabel.text = @"0";
	}
	return _fanNumebrLabel;
}

- (UIButton *)leftButton {
	if (!_leftButton) {
		_leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_leftButton addTarget:self action:@selector(pushImcome) forControlEvents:UIControlEventTouchUpInside];
	}
	return _leftButton;
}

- (UIButton *)rightButton {
	if (!_rightButton) {
		_rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
		[_rightButton addTarget:self action:@selector(pushFansVC) forControlEvents:UIControlEventTouchUpInside];
	}
	return _rightButton;
}
@end
